//Start functions of Operation
var OperationTemplate = "templates/Operation.xsl"
var qtRaisedException = 0

function loadOperationData(xID){
	if(objectHTTPXML) {
		xIDTmp = xID
		waitBox = new Lightbox.base('box1');
		objectHTTPXML.open('GET', '/model.load.operation?id=' + xID + '&m_id=' + modelID , true)
		objectHTTPXML.onreadystatechange = returnOperationData
		objectHTTPXML.send(null)
	} else {
		alert("Your browser don't suport Ajax")
	}
}

function returnOperationData(){
	if (objectHTTPXML.readyState == 4) {
		xml = objectHTTPXML.responseXML
		template = loadXSLfromXML(OperationTemplate, xml)
		document.getElementById("divBody").innerHTML = template
		waitBox.hideBox(null);
		insertTypeOp();
	}			
}

function saveOperation(xID){
	if(objectHTTPXML) {
		var parameters = "m_id=" + modelID +
						 "&id=" + encodeURI(document.getElementById("id").value) +
						 "&name=" + encodeURI(document.getElementById("name").value) +
						 "&lower=" + encodeURI(document.getElementById("lower").value) +
						 "&upper=" + encodeURI(document.getElementById("upper").value) +
						 "&type=" + encodeURI(document.getElementById("type").value)
		
		if(document.getElementById("isOrdered").checked){
			parameters += "&isOrdered=" + 1
		}
		if(document.getElementById("isUnique").checked){
			parameters += "&isUnique=" + 1
		}
		
		var raisedException = document.getElementsByName("raisedException")
		
		if(raisedException != null){
			for(var i = 0; i < raisedException.length; ++i){
				parameters += "&raisedException=" + encodeURI(raisedException[i].value)				
			}
		}

		xIDTmp = xID

		waitBox = new Lightbox.base('box1');

		objectHTTPXML.onreadystatechange = returnSaveOperation
		objectHTTPXML.open('POST', '/model.save.operation', true);
		objectHTTPXML.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
		objectHTTPXML.setRequestHeader("Content-length", parameters.length);
		objectHTTPXML.setRequestHeader("Connection", "close");
		objectHTTPXML.send(parameters);
	} else {
		alert("Your browser don't suport Ajax")
	}
}

function returnSaveOperation(){
	if (objectHTTPXML.readyState == 4) {
		waitBox.hideBox(null);

		if(objectHTTPXML.responseText == 'OK'){
			changeName(xIDTmp, document.getElementById("name").value)
			alert('Data saved!')
		} else {
			alert('Data not saved!')
			loadOperationData(xIDTmp)
		}
	}			
}

function createOperation(xID){
	if(objectHTTPXML) {
		xIDTmp = xID
		waitBox = new Lightbox.base('box1');
		objectHTTPXML.open('GET', '/model.create.operation?&m_id=' + modelID + '&p_id=' + xID , true)
		objectHTTPXML.onreadystatechange = returnOperation
		objectHTTPXML.send(null)
	} else {
		alert("Your browser don't suport Ajax")
	}
}

function returnOperation(){
	if (objectHTTPXML.readyState == 4) {
		if(objectHTTPXML.responseText.split("$$$$").length != 0){
			id = objectHTTPXML.responseText.split("$$$$")
			createNewNode(xIDTmp, id[0], id[1], 'javascript:loadOperationData("' + id[0] + '");', 'operation.gif', 'operation.gif')
		} else {
			alert('Error to create the Operation!')
		}
		waitBox.hideBox(null);
	}			
}

function insertRaisedException(id){
	var parameters = Array()

	parameters[0] = window
	parameters[1] = modelID

	var re = Array()	
	var raisedException = document.getElementsByName("raisedException")
	if(raisedException != null){
		for(var i = 0; i < raisedException.length; ++i){
			re[i] = raisedException[i].value				
		}
	}
	
	parameters[2] = re
	
	showModalDialog("/templates/getRaisedException.html",parameters,"toolbar:false,status:false;dialogWidth:300px;dialogHeight:150px");
}

function insertRaisedExceptionAux(id, name){
	div = document.getElementById('divRaisedException')
	
	hidden = document.createElement("input")
	hidden.type = "hidden"
	hidden.name = "raisedException"
	hidden.value = id

	text1 = document.createTextNode(name + " - ")

	a = document.createElement("a")
	a.href="javascript:deleteRaisedException('" + "raisedException" + qtRaisedException + "')"
	a.innerHTML = "Delete"
	
	div2 = document.createElement("div")
	div2.id = "raisedException" + qtRaisedException
	div2.appendChild(hidden)
	div2.appendChild(text1)
	div2.appendChild(a)

	div.appendChild(div2)

	qtRaisedException++
	
}

function deleteRaisedException(id){
	div = document.getElementById('divRaisedException')
	div2 = document.getElementById(id)
	div.removeChild(div2)
	qtRaisedException--
}

function insertTypeOp(){
	if(objectHTTPXML) {
		waitBox = new Lightbox.base('box1');
		objectHTTPXML.open('GET', '/model.load.alltypes?&m_id=' + modelID, true)
		objectHTTPXML.onreadystatechange = returnInsertTypeOp
		objectHTTPXML.send(null)
	} else {
		alert("Your browser don't suport Ajax")
	}
}

function returnInsertTypeOp(){
	if (objectHTTPXML.readyState == 4) {
		xml = objectHTTPXML.responseXML
		template = loadXSLfromXML(getTypesTemplate, xml)
		document.getElementById("divType").innerHTML = template

		var typeId = document.getElementById("typeId").value
		var type = document.getElementById("type")
		
		for(i = 0; i < type.options.length; ++i){
			if(type.options[i].value == typeId){
				type.selectedIndex = i
				break
			}
		}
		
		waitBox.hideBox(null);
	}			
}
//End functions of Operation